#include "GameOptionsState.h"

GameOptionsState::GameOptionsState(BeanieCore* a_core, bool a_renew)
{
	m_core = a_core;

	m_name = "GameOptions";
	m_selfId = GAMEOPTIONS;
	m_swapId = GAMEOPTIONS;
	m_completed = false;
	m_renew = a_renew;
}

void GameOptionsState::drawSndBars()
{
	//Master volume
	for(int i=0;i<10;i++){
		if(i<=maVolume){
			m_sndbar[i].draw();
		}
	}
	//Music volume
	for(int i=10;i<20;i++){
		if(i-10<=mVolume){
			m_sndbar[i].draw();
		}
	}
	//SFX volume
	for(int i=20;i<30;i++){
		if(i-20<=sxVolume){
			m_sndbar[i].draw();
		}
	}
}

void GameOptionsState::updateSndBars()
{
	for(int i=0;i<30;i++)
	{
		m_sndbar[i].update();
	}
}

void GameOptionsState::initSndBars()
{	
	float x1 = 630.0f;

	for(int i=0; i<10; i++){
		m_sndbar[i].initTexture(m_core,IMG_SNDBAR,
			TEX_TOPLEFT,V2DF(x1,265.0f),V2D_ONES,0.0f,true);
		x1 += 10.0f;
	}
	x1 = 630;
	for(int i=10; i<20; i++){
		m_sndbar[i].initTexture(m_core,IMG_SNDBAR,
			TEX_TOPLEFT,V2DF(x1,350.0f),V2D_ONES,0.0f,true);
		x1 += 10.0f;
	}
	x1 = 630;
	for(int i=20; i<30; i++){
		m_sndbar[i].initTexture(m_core,IMG_SNDBAR,
			TEX_TOPLEFT,V2DF(x1,432.0f),V2D_ONES,0.0f,true);
		x1 += 10.0f;
	}
}

void GameOptionsState::init()
{
	maVolume=10;
	mVolume=10;
	sxVolume=10;

	initSndBars();

	m_object.initTexture(m_core,BG_OPTIONS,
		TEX_TOPLEFT,V2D_ZERO,V2D_ONES,0.0f,true);
	m_savebtn.initTexture(m_core,BTN_SAVE,
		TEX_TOPLEFT,V2DF(405,508),V2D_ONES,0.0f,true);
	m_cancelbtn.initTexture(m_core,BTN_CANCEL,
		TEX_TOPLEFT,V2DF(657,507),V2D_ONES,0.0f,true);
	m_arrowbtnMaL.initTexture(m_core,IMG_ARROW,
		TEX_TOPLEFT,V2DF(747,256),V2D_ONES,0.0f,true);
	m_arrowbtnMaR.initTexture(m_core,IMG_ARROW,
		TEX_TOPLEFT,V2DF(801,256),V2D_ONES,0.0f,true);
	m_arrowbtnML.initTexture(m_core,IMG_ARROW,
		TEX_TOPLEFT,V2DF(748,342),V2D_ONES,0.0f,true);
	m_arrowbtnMR.initTexture(m_core,IMG_ARROW,
		TEX_TOPLEFT,V2DF(802,342),V2D_ONES,0.0f,true);
	m_arrowbtnSXL.initTexture(m_core,IMG_ARROW,
		TEX_TOPLEFT,V2DF(748,424),V2D_ONES,0.0f,true);
	m_arrowbtnSXR.initTexture(m_core,IMG_ARROW,
		TEX_TOPLEFT,V2DF(802,424),V2D_ONES,0.0f,true);

	//init the GameButtons onClick() action
	//takes in a Lambda Expression that indicates what to do when the button is click
	//in this case, it sets the state to the desire state and trigger the state to
	//be in completed state which is checked by the system for swapping to the given swapId
	m_savebtn.onClick([&](){m_swapId=GAMEMENU;m_completed=true;});
	m_cancelbtn.onClick([&](){m_swapId=GAMEMENU;m_completed=true;});
	m_arrowbtnMaL.onClick([&](){if(maVolume>0)maVolume-=1;});
	m_arrowbtnMaR.onClick([&](){if(maVolume<10)maVolume+=1;});
	m_arrowbtnML.onClick([&](){if(mVolume>0)mVolume-=1;});
	m_arrowbtnMR.onClick([&](){if(mVolume<10)mVolume+=1;});
	m_arrowbtnSXL.onClick([&](){if(sxVolume>0)sxVolume-=1;});
	m_arrowbtnSXR.onClick([&](){if(sxVolume<10)sxVolume+=1;});
}

void GameOptionsState::release()
{

}

void GameOptionsState::input()
{
	m_savebtn.input();
	m_cancelbtn.input();
	m_arrowbtnMaL.input();	m_arrowbtnMaR.input();
	m_arrowbtnML.input();	m_arrowbtnMR.input();
	m_arrowbtnSXL.input();	m_arrowbtnSXR.input();
}

void GameOptionsState::update()
{
	//update the textures for the main menu
	m_object.update();
	updateSndBars();
	m_sndbar[0].update();
	m_savebtn.update();
	m_cancelbtn.update();
	m_arrowbtnMaL.update();	m_arrowbtnMaR.update();
	m_arrowbtnML.update();	m_arrowbtnMR.update();
	m_arrowbtnSXL.update();	m_arrowbtnSXR.update();
}

void GameOptionsState::draw()
{
	//draw the textures
	m_object.draw();
	drawSndBars();
	m_savebtn.draw();
	m_cancelbtn.draw();
	m_arrowbtnMaL.draw();	m_arrowbtnMaR.draw();
	m_arrowbtnML.draw();	m_arrowbtnMR.draw();
	m_arrowbtnSXL.draw();	m_arrowbtnSXR.draw();
}

void GameOptionsState::preLoop()
{
	
}

void GameOptionsState::postLoop()
{

}